package request

import (
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
	"lab.com/consts"
	"lab.com/define/errorcode"
)

// NewsListReq 新闻公告列表查询参数
type NewsListReq struct {
	Title     string `json:"title" form:"title" comment:"关键字"`
	Type      int    `json:"type" form:"type" validate:"oneof=0 10 20" comment:"新闻公告类型"`
	IsAll     int    `json:"is_all" form:"is_all" validate:"oneof=0 10" comment:"是否是全部，0全部 10首页"`
	StartTime string `json:"start_time" form:"start_time" comment:"开始时间"`
	EndTime   string `json:"end_time" form:"end_time" comment:"结束时间"`
	PageOffset
}

// NewsBasicInfo 新闻公告列表信息
type NewsBasicInfo struct {
	Id          int    `json:"id"`
	Title       string `json:"title"`
	Abstract    string `json:"abstract"`
	Type        int    `json:"type"`
	Author      string `json:"author"`
	ImgPath     string `json:"img_path"`
	PublishTimt string `json:"publish_time"`
}

// NewsListRes 新闻公告列表返回结果
type NewsListRes struct {
	List  []NewsBasicInfo `json:"list"`
	Count int             `json:"count"`
}

// CheckInputParams return input params
func (news *NewsListReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(news); err != nil {
		return err
	}
	if errs, err := Validate(news); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	news.ParseOffset()
	return nil
}

// NewsDetailReq 新闻公告详情查询参数
type NewsDetailReq struct {
	ID int `json:"id" form:"id" validate:"required" comment:"新闻公告id"`
}

// NewsDetailRes 新闻公告详情返回结果
type NewsDetailRes struct {
	Id          int    `json:"id"`
	Title       string `json:"title"`
	Abstract    string `json:"abstract"`
	Type        int    `json:"type"`
	Author      string `json:"author"`
	ImgPath     string `json:"img_path"`
	IsShow      int    `json:"is_show"`
	IsHome      int    `json:"is_home"`
	PublishTimt string `json:"publish_time"`
	Content     string `json:"content"`
	ClickTimes int `json:"click_times"`
}

// CheckInputParams return input params
func (news *NewsDetailReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(news); err != nil {
		return err
	}
	if errs, err := Validate(news); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	return nil
}

// NewsAddReq 新闻公告新增参数
type NewsAddReq struct {
	Title       string `json:"title" form:"title" validate:"required,max=50" comment:"标题"`
	Abstract    string `json:"abstract" form:"abstract" validate:"required,max=100" comment:"摘要"`
	Author      string `json:"author" form:"author" validate:"required,max=20" comment:"作者"`
	Content     string `json:"content" form:"content" comment:"内容"`
	Type        int    `json:"type" form:"type" validate:"oneof=0 10 20" comment:"新闻公告类型"`
	PublishTime string `json:"publish_time" form:"publish_time" validate:"required" comment:"发布时间"`
	IsShow      int    `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
	IsHome      int    `json:"is_home" form:"is_home" validate:"oneof=0 10" comment:"是否显示在首页，10是，0不是"`
	ImgPath     string `json:"img_path" form:"img_path" validate:"required,max=255" comment:"主图"`
}

// CheckInputParams return input params
func (news *NewsAddReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(news); err != nil {
		return err
	}
	if errs, err := Validate(news); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// NewsDeleteReq 新闻公告删除参数
type NewsDeleteReq struct {
	ID int `json:"id" form:"id" validate:"required" comment:"新闻公告id"`
}

// CheckInputParams return input params
func (news *NewsDeleteReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(news); err != nil {
		return err
	}
	if errs, err := Validate(news); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// NewsEditReq 新闻公告编辑参数
type NewsEditReq struct {
	ID          int    `json:"id" form:"id" validate:"required" comment:"新闻公告id"`
	Title       string `json:"title" form:"title" validate:"required,max=50" comment:"标题"`
	Abstract    string `json:"abstract" form:"abstract" validate:"required,max=100" comment:"摘要"`
	Author      string `json:"author" form:"author" validate:"required,max=20" comment:"作者"`
	Content     string `json:"content" form:"content" comment:"内容"`
	Type        int    `json:"type" form:"type" validate:"oneof=0 10 20" comment:"新闻公告类型"`
	PublishTime string `json:"publish_time" form:"publish_time" validate:"required" comment:"发布时间"`
	IsShow      int    `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
	IsHome      int    `json:"is_home" form:"is_home" validate:"oneof=0 10" comment:"是否显示在首页，10是，0不是"`
	ImgPath     string `json:"img_path" form:"img_path" validate:"required,max=255" comment:"主图"`
}

// CheckInputParams return input params
func (news *NewsEditReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(news); err != nil {
		return err
	}
	if errs, err := Validate(news); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// NewsShowReq 新闻公告展示参数
type NewsShowReq struct {
	ID     int `json:"id" form:"id" validate:"required" comment:"新闻公告id"`
	IsShow int `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
}

// CheckInputParams return input params
func (news *NewsShowReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(news); err != nil {
		return err
	}
	if errs, err := Validate(news); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// NewsHomeReq 新闻公告首页展示参数
type NewsHomeReq struct {
	ID     int `json:"id" form:"id" validate:"required" comment:"新闻公告id"`
	IsHome int `json:"is_home" form:"is_home" validate:"oneof=0 10" comment:"是否显示在首页，10是，0不是"`
}

// CheckInputParams return input params
func (news *NewsHomeReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(news); err != nil {
		return err
	}
	if errs, err := Validate(news); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// NewsListInManageReq 新闻公告管理列表查询参数
type NewsListInManageReq struct {
	Title string `json:"title" form:"title" comment:"关键字"`
	Type  int    `json:"type" form:"type" validate:"oneof=0 10 20" comment:"新闻公告类型"`
	// IsShow    int    `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
	// IsHome    int    `json:"is_home" form:"is_home" validate:"oneof=0 10" comment:"是否显示在首页，10是，0不是"`
	StartTime string `json:"start_time" form:"start_time" comment:"开始时间"`
	EndTime   string `json:"end_time" form:"end_time" comment:"结束时间"`
	PageOffset
}

// NewsInfo 新闻公告列表信息
type NewsInfo struct {
	Id          int    `json:"id"`
	Title       string `json:"title"`
	Abstract    string `json:"abstract"`
	Type        int    `json:"type"`
	Author      string `json:"author"`
	ImgPath     string `json:"img_path"`
	IsShow      int    `json:"is_show"`
	IsHome      int    `json:"is_home"`
	PublishTimt string `json:"publish_time"`
}

// NewsListInManageRes 新闻公告列表返回结果
type NewsListInManageRes struct {
	List  []NewsInfo `json:"list"`
	Count int        `json:"count"`
}

// CheckInputParams return input params
func (news *NewsListInManageReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(news); err != nil {
		return err
	}
	if errs, err := Validate(news); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}

	news.ParseOffset()
	return nil
}
